var fruits={
    "apples":0.69,
    "oranges":0.59,
    "bananas":0.39
}

function isNumber(n){
    return !isNaN(parseFloat(n))&&isFinite(n);
}
function validate(n){
    if(n=="")return;
    if(!isNumber(n))alert("INVALID INPUT. PLEASE INPUT A NUMBER");
}
function blur(o){
    o.blur();
}
function updateTotal(){
    var total=document.getElementById("total");
    totalCost=0;
    for (f in fruits){
        var fruit=document.getElementById(f).value;
        if (fruit=="") continue;
        if(!isNumber(fruit)){
            total.value="NAN";
            return;
        }
        totalCost+=fruits[f]*document.getElementById(f).value;
    }
    if(totalCost==0){
        total.value=""
        return;
    }
    total.value=totalCost.toFixed(2);
}
function validateForm()
{
    var name=document.forms["fruitForm"]["username"].value;
    var total=document.forms["fruitForm"]["total"].value;
    if (name==null||name==""){
        alert("Username must be filled");
        return false;
    }        
    for (f in fruits){
        var fruit=document.forms["fruitForm"][f].value;
        if (fruit=="")continue;
        if (!isNumber(fruit)){
            alert(f+" should be filled with number");
            return false;
        }
    }
    if (total==""){
        alert("Please order some fruits");
        return false;
    }
    if(document.querySelector('input[name="payment"]:checked')==null){
        alert("Please Select a payment method");
        return false;
    }
    return true;
}
